package com.chanel.iamframework.core.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.chanel.iamframework.core.dao.ApplicationDAO;
import com.chanel.iamframework.core.domain.Application;
import com.chanel.iamframework.core.domain.Group;
import com.chanel.iamframework.core.service.ApplicationService;

public class ApplicationServiceImpl implements ApplicationService {

	private static final Logger logger = LoggerFactory.getLogger(ApplicationServiceImpl.class);
	
	private ApplicationDAO applicationDAO;
	
	public ApplicationServiceImpl(@NotNull ApplicationDAO applicationDAO) {
		this.applicationDAO = applicationDAO;
	}
	
	@Override
	public Application getApplication(Long applicationId) {
		return applicationDAO.getApplication(applicationId);
	}

	@Override
	public void createApplication(Application application) {
		applicationDAO.createApplication(application);
	}

	@Override
	public boolean deleteApplication(Long applicationId) {
		return applicationDAO.deleteApplication(applicationId) > 0 ? true : false;
	}

	@Override
	public boolean updateApplication(Application application) {
		return applicationDAO.updateApplication(application) > 0 ? true : false;
	}

	@Override
	public Application loadGroups(Long applicationId) {
		return applicationDAO.loadGroups(applicationId);
	}

	@Override
	public Application loadRoles(Long applicationId) {
		return applicationDAO.loadRoles(applicationId);
	}

	@Override
	public Application loadUsers(Long applicationId) {
		return applicationDAO.loadUsers(applicationId);
	}

	@Override
	public void addGroups(Application application) {
		if (CollectionUtils.isEmpty(application.getGroups())) {
			logger.debug("application.groups is empty");
		} else {
			logger.debug("application.groups contain {} groups", application.getGroups().size());
			for (Group group: application.getGroups()) {
				applicationDAO.addGroup(application.getApplicationId(), group.getGroupId());
				logger.debug("association ({}, {}) created", application, group);
			}
		}
	}

	@Override
	public boolean deleteGroups(Application application) {
		boolean deletedGroups = false;
		
		if (CollectionUtils.isEmpty(application.getGroups())) {
			logger.debug("application.groups is empty");
		} else if (applicationDAO.getApplication(application.getApplicationId()) == null) {
			logger.debug("applicationId={} not exist", application.getApplicationId());
		} else {
			List<Group> groupsToBeDeleted = new ArrayList<Group>();
			
			logger.debug("application.groups contain {} groups", application.getGroups().size());
			for (Group group: application.getGroups()) {
				if (applicationDAO.deleteGroup(application.getApplicationId(), group.getGroupId()) == 1) {
					groupsToBeDeleted.add(group);
					logger.debug("association ({}, {}) deleted", application, group);
				} else {
					logger.debug("association ({}, {}) not found", application, group);
				}
			}
			
			application.getGroups().removeAll(groupsToBeDeleted);
			deletedGroups = groupsToBeDeleted.size() > 0;
		}
		
		return deletedGroups;
	}

}
